package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.input.WorkHoursRequestHstByStatePage;
import com.be.beadmin.product.dto.input.WorkHoursRequestHstByTaskIdPage;
import com.be.beadmin.product.dto.input.WorkHoursRequestHstDto;
import com.be.beadmin.product.dto.output.WorkHoursRequestHstEx;
import com.be.beadmin.product.service.WorkHoursRequestHstService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author ljh
* @date 2023-04-18
**/
@Api(tags = {"工时补录（审核）管理"})
@RestController
public class WorkHoursRequestHstController {

    @Autowired
    private  WorkHoursRequestHstService workHoursRequestHstService;

    @ApiOperation("工时补录审核分页信息")
    @PostMapping(value = "/product/getWorkHoursRequestHstByPage")
    public ResultModel getWorkHoursRequestHstByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(workHoursRequestHstService.getWorkHoursRequestHstByPage(pageEntity)) ;
    }

    @ApiOperation("工时补录审核分页信息（By TaskId 生产工序id）")
    @PostMapping(value = "/product/getWorkHoursRequestHstByPageAndTaskId")
    public ResultModel getWorkHoursRequestHstByPageAndTaskId(@RequestBody WorkHoursRequestHstByTaskIdPage workHoursRequestHstByTaskIdPage)  {

        try {
            return ResultModel.success(workHoursRequestHstService.getWorkHoursRequestHstByPageAndTaskId(workHoursRequestHstByTaskIdPage)) ;
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }

    }

    @ApiOperation("工时补录审核分页信息（By State 审核状态 ）")
    @PostMapping(value = "/product/getWorkHoursRequestHstByPageAndState")
    public ResultModel getWorkHoursRequestHstByPageAndState(@RequestBody WorkHoursRequestHstByStatePage workHoursRequestHstByStatePage)  {

        try {
            return ResultModel.success(workHoursRequestHstService.getWorkHoursRequestHstByPageAndState(workHoursRequestHstByStatePage)) ;
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }


    }


//    @PostMapping("/product/addWorkHoursRequestHst")
//    public ResultModel addWorkHoursRequestHst(@RequestBody WorkHoursRequestHstDto workHoursRequestHstDto){
//        workHoursRequestHstService.addWorkHoursRequestHst(workHoursRequestHstDto);
//        return ResultModel.success(ResultModel.ADD);
//    }
//
    
    @PostMapping("/product/delWorkHoursRequestHst")
    public ResultModel delWorkHoursRequestHst(@RequestBody WorkHoursRequestHstDto workHoursRequestHstDto){
        workHoursRequestHstService.delWorkHoursRequestHst(workHoursRequestHstDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("工时补录审核")
    @PostMapping("/product/updateWorkHoursRequestHst")
    public ResultModel updateWorkHoursRequestHst(@RequestBody WorkHoursRequestHstDto workHoursRequestHstDto){
        workHoursRequestHstService.updateWorkHoursRequestHst(workHoursRequestHstDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportWorkHoursRequestHst")
    public void exportWorkHoursRequestHst(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<WorkHoursRequestHstEx> workHoursRequestHstPage = workHoursRequestHstService.getWorkHoursRequestHstByPage(pageEntity);
        List<WorkHoursRequestHstEx> workHoursRequestHstExes = workHoursRequestHstPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("WorkHoursRequestHst接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), WorkHoursRequestHstEx.class).sheet("sheet1").doWrite(workHoursRequestHstExes);
    }
}